// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_main");
	var dialog = require("dialog");
	var databind = require("databind");
	var validate = require("validate");
	var css_detail_info = require("./financingDetailInfo.css");
	var html_cancelApplication = require("./html_cancelApplication.html");
	var enterpriseId;
	var abnormal;
	var tools = require("tools");
	var firstVisited = 0;
	var constant = require("constant")
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var financeId = "";
	var init = function() {
		firstVisited = base.getUrlParam("firstVisited");
		
		inquireFinanceState();
		
		footer.init();
		$("#footer").removeClass("none");
		enterpriseId = base.getUrlParam("enterpriseId");
		abnormal = base.getUrlParam("abnormal");
		leftMenu.init(5);
		topMenu.init(1);
		$(".footer").removeClass("none");
		$("#apply").addClass("leftMenu-active");
		$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
			if (enterpriseId != null) {
				inquireFinanceList(enterpriseId);
			} else {
				inquireFinanceList();
			}
		}

		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#main").on("click", "#financeSearch", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireFinanceList();
		});
		//收款单查询
		$(".receivableNoticeList").unbind("click").click(function() {
			location.href = "../financing/receivableNoticeList.html";
		});
		//已完成融资
		$(".endFinanceList").unbind("click").click(function() {
			location.href = "../financing/endFinanceList.html";
		});
		//收货通知查询
		$(".recieveNoticeList").unbind("click").click(function() {
			location.href = "../financing/recieveNoticeList.html";
		});
		//放货查询
		$(".deliveryNoticeList").unbind("click").click(function() {
			location.href = "../financing/deliveryNoticeList.html";
		});
		//放款单查询
		$(".loanNoticeList").unbind("click").click(function() {
			location.href = "../financing/loanNoticeList.html";
		});
		//左侧菜单栏	解除监管
		$(".cancelSuperviseA").unbind("click").click(function() {
			router.route("main/supervisionRelieveList.html?left=9", true);
		});

		//左侧菜单栏	待审批
		$(".financeUnexamA").unbind("click").click(function() {
			router.route("main/main_finance.html?left=0&financeState=1", true);
		});

		//左侧菜单栏	合同上传
		$(".contractUnsignA").unbind("click").click(function() {
			router.route("main/main_finance.html?left=1&financeState=3", true);
		});

		//左侧菜单栏	收货通知
		$(".noticeReceiveA").unbind("click").click(function() {
			router.route("main/main_finance.html?left=2&financeState=5", true);
		});

		//左侧菜单栏 放款确认
		$(".financeUnreleaseA").unbind("click").click(function() {
			router.route("main/loanNotice_list.html?left=4", true);
		});

		//左侧菜单栏  放货通知
		$(".financeUnreceiveBulkstockA").unbind("click").click(function() {
			router.route("main/deliveryNotice_list.html?left=8", true);
		});

		//左侧菜单栏 收款确认
		$(".financeUnreceiveA").unbind("click").click(function() {
			router.route("main/receivablesNotice_list.html?left=7", true);
		});

		//左侧菜单栏 通知运输
		$(".financeUntransportA").unbind("click").click(function() {
			router.route("main/transportNotice_list.html?left=5", true);
		});


		//左侧菜单栏 提货审批
		$(".financePickupExamA").unbind("click").click(function() {
			router.route("main/pickupList.html?left=6", true);
		});

		//左侧菜单栏 监管确认
		$(".financeUnsuperviseA").unbind("click").click(function() {
			router.route("main/supervisionNotice_list.html?left=3", true);
		});

		//左侧菜单栏 延期计息
		$(".interestDelayA").unbind("click").click(function() {
			router.route("main/interestDelayList.html?left=12", true);
		});

		//左侧菜单栏 延期还款
		$(".pickupDelayA").unbind("click").click(function() {
			router.route("main/pickupDelayList.html?left=11", true);
		});

		//左侧菜单栏 提前还款
		$(".pickupPreA").unbind("click").click(function() {
			router.route("main/pickupPreList.html?left=10", true);
		});

		//左侧菜单栏 赎货申请
		$(".pickupRansomA").unbind("click").click(function() {
			router.route("main/pickupRansomList.html?left=14", true);
		});

		//左侧菜单栏 扣货申请
		$(".detainedA").unbind("click").click(function() {
			router.route("main/detainedList.html?left=13", true);
		});

		//左侧菜单栏 扣货申请
		$(".badassetsHandleA").unbind("click").click(function() {
			router.route("main/badassetsHandleList.html?left=15", true);
		});

		//左侧菜单栏 异常管理
		$(".abnormalManageA").unbind("click").click(function() {
			//			router.route("exception/exceptionManage.html?left=16", true);
			location.href = "../exception/exceptionManage.html";
		});
	};
	
	//查询融资状态列表
	var inquireFinanceState = function() {
		base.fnAjax({
			action: "inquireFinanceState.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var financeState = (data.datas[0].financeStateStr).split(',');
					$("#financeState").html('<option value="">全部</option>');
					for (var i = 0; i < financeState.length; i++) {
						var str = '<option value="'+ i +'">' + financeState[i] + '</option>'
						$("#financeState").append(str);
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
		
	};

	//改变融资状态背景色
	var changeStatusColor = function() {
		var status = $(".order_status");
		for (var i = 0, j = status.length; i < j; i++) {
			switch ($.trim(status[i].innerHTML)) {
				case "融资":
					$(status[i]).css("background-color", "#6BA8E7");
					break;
				case "换货":
					$(status[i]).css("background-color", "#28BF85");
					break;
			}
		}
	};

	//查询融资信息列表
	var inquireFinanceList = function(enterpriseId) {
		if ($("#financeState").val() != -1) {
			var financeState = $("#financeState").val();
		} else {
			var financeState;
		}
		var beginDate = $("#applyTimeStart").val().replace(new RegExp(/-/g), '');
		var endDate = $("#applyTimeEnd").val().replace(new RegExp(/-/g), '');
		var financeNumber = $("#financeNumber").val();
		var maxAmount = $("#maxAmount").val();
		var minAmount = $("#minAmount").val();
		var commodityName = $("#commodityName").val();
		var enterpriseName = $("#enterpriseName").val();
		var financeContractSerialNumber = $("input[name=financeContractSerialNumber]").val();
		var waybillNumber = $("input[name=waybillNumber]").val();
		var caseNumber = $("input[name=caseNumber]").val();
		var productType = $(".wrapper select[name=productType]").val();

		base.fnAjax({
			action: "inquireFinanceList.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				financeState: financeState,
				beginDate: beginDate,
				endDate: endDate,
				financeNumber: financeNumber,
				maxAmount: maxAmount,
				minAmount: minAmount,
				bulkstockName: commodityName,
				enterpriseId: enterpriseId,
				abnormal: abnormal,
				enterpriseName: enterpriseName,
				financeContractSerialNumber: financeContractSerialNumber,
				waybillNumber: waybillNumber,
				caseNumber: caseNumber,
				productType: productType
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					if (abnormal == 0) {
						var tpl = $("#financeListTpl1").html();
					} else {
						var tpl = $("#financeListTpl").html();
					}
					var html = juicer(tpl, data);
					$("#financeListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							if (enterpriseId != null) {
								inquireFinanceList(enterpriseId);
							} else {
								inquireFinanceList();
							}
						});
					changeStatusColor();
					$("#main").on("click", "#detail", function() {
						var financeId = [];
						var $this = $(this).parents("tr.tr_style");
						financeId.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeId, true);
					});
					
					$("#main").on("click", "#costing", function() {
						var financeId = [];
						var $this = $(this).parents("tr.tr_style");
						financeId.push($this.attr("financeId"));
						router.route("financing/financingDetailCost.html?financeId=" + financeId, true);
					});
					
					$("#financingManage .bulkstockListing").unbind("click").click(function(){
						router.route("bulkstockListing/accountBulkstockListing.html?financeId=" + $(this).parents("tr").attr("financeId") + "&productType=" + $(this).parents("tr").attr("productType"));
					});
					
					$("#financingManage .calcelApply").unbind("click").click(function(){
						var $this = $(this);
						var ii = layer.open({
						  title: '融资取消申请',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '300px'], //宽高
						  content: html_cancelApplication,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".createFinanceCancelApplication"), function(){
						  		layer.msg("申请成功！");
						  		layer.close(ii);
						  		location.href = "../financing/financingManage.html";
						  	});
						  }
						});
						
						$(".createFinanceCancelApplication input[name=financeId]").val($this.parents("tr").attr("financeId"));
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	return {
		init: init
	};
});